package com.chinaztt.fda.test;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.TextView;

import com.chinaztt.fda.adapter.HackyViewPager;
import com.chinaztt.fda.adapter.PhotoAdapter;
import com.chinaztt.fda.entity.PhotoBean;
import com.chinaztt.fda.ui.R;

import java.util.List;

/**
 * Description:
 * Date: 2016-02-01  14:03
 * User: wushan
 */
public class PhotoShowActivtiy extends Activity implements View.OnClickListener {

    private String title;

    private TextView titleTv;
    private TextView imageNumTv;

    //    private ArrayList<String> pic_path_list;    //图片本地路径
//    private ArrayList<String> pic_url_list;     //图片网络路径
    private List<String> list;

    private HackyViewPager viewPager;

    private PhotoAdapter adapter;

    private boolean modified;// 是否有修改


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_pager);
        initView();
    }

    protected void initView() {
        Intent intent = getIntent();
        title = intent.getStringExtra("title");
        PhotoBean bean = (PhotoBean) intent.getSerializableExtra("data");
        list = bean.getData();
        initAll();
    }

    private int positionNow = 0;

    private void initAll() {
        ((TextView) findViewById(R.id.tv_title_name)).setText(title);

        findViewById(R.id.title_back).setOnClickListener(this);
        setPagerTitle(0);
        TextView editTv = (TextView) findViewById(R.id.update_db);
        editTv.setVisibility(View.VISIBLE);
        editTv.setText("删除");
        editTv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.ic_launcher,
                0, 0, 0);
        editTv.setOnClickListener(this);

        imageNumTv = (TextView) findViewById(R.id.image_pager_num_tv);
        setImageNum(0);

        viewPager = (HackyViewPager) findViewById(R.id.pager);
        adapter = new PhotoAdapter(list);
        viewPager.setAdapter(adapter);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                positionNow = position;
                setImageNum(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });

    }


    private void setImageNum(int position) {
//        int size = pic_url_list != null ? pic_url_list.size() : pic_path_list.size();
        int size = list.size();
        imageNumTv.setText(position + 1 + "/" + size);
    }

    /**
     * 设置标题
     *
     * @param index
     */
    private void setPagerTitle(int index) {
//        int size = pic_url_list != null ? pic_url_list.size() : pic_path_list.size();
        int size = list.size();
        setTitle(title + " " + (index + 1) + "/" + size);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_back:// 后退
                if (modified) {// 有修改
                    result0k();// 返回数据
                } else {
                    setResult(RESULT_CANCELED);
                }
                finish();
                break;

            case R.id.update_db:// 编辑 删除
                showDelDialog();
                break;
        }
    }

    private void result0k() {
        Intent data = new Intent();
//        data.putStringArrayListExtra("pic_path_list", pic_path_list);
//        data.putStringArrayListExtra("pic_url_list", pic_url_list);
        data.putExtra("data", (Parcelable) list);

        setResult(RESULT_OK, data);
    }

    private AlertDialog.Builder delDialog;
    private DialogInterface.OnClickListener delDialogListener = new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (which == DialogInterface.BUTTON_POSITIVE) {

//                if (pic_url_list != null && pic_url_list.size() > 0) {
//                    pic_url_list.remove(positionNow);
//                } else if (pic_path_list != null && pic_path_list.size() > 0) {
//                    pic_path_list.remove(positionNow);
//                }

                if (list != null && list.size() > 0) {
                    list.remove(positionNow);
                }

                adapter.notifyDataSetChanged();
                if (positionNow >= adapter.getCount() && adapter.getCount() > 0) {
                    positionNow = adapter.getCount() - 1;
                }
                viewPager.setCurrentItem(positionNow);
                setPagerTitle(viewPager.getCurrentItem());
                setImageNum(positionNow);
                modified = true;

                if (list != null && list.size() == 0) {
                    result0k();
                    finish();
                }
            }
        }
    };

    /**
     * 显示删除对话框
     */
    private void showDelDialog() {
        if (delDialog == null) {
            delDialog = new AlertDialog.Builder(this);
            delDialog.setTitle("提示");
            delDialog.setMessage("确认删除照片");
            delDialog.setPositiveButton("确认", delDialogListener);
            delDialog.setNegativeButton("取消", null);
        }
        delDialog.show();
    }


}
